package mysql

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"sync"
	"usershop/good_srv/appconfig"
)

var (
	DB   *gorm.DB
	err  error
	once sync.Once
)

func InitMysql() {
	mah := appconfig.AppData.MySQLConfig
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", mah.Name,
		mah.Pwd, mah.Head, mah.Pore, mah.DBName)
	//dsn := "user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local"
	once.Do(func() {
		DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	})

	if err != nil {
		fmt.Println("数据库链接失败")
		return
	}
	fmt.Println("数据库链接成功")
	DB.AutoMigrate(&Goods{})

}
